package com.software.levelupandroid.taskIssue.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.software.levelupandroid.R;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.utils.DateUtil;
import com.software.levelupandroid.utils.DialogUtil;


/**
 * @Description TODO
 * @Author LiangYaJie
 * @CreateTime 2024年05月09日 15:06:13
 */
public class ReminderTimeAdapter extends RecyclerView.Adapter<ReminderTimeAdapter.ViewHolder> {
    private int size;
    private Context context;
    private Task task;

    public ReminderTimeAdapter(Context context, int size, Task task) {
        this.context = context;
        this.size = size;
        this.task = task;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_timing_task_reminder, parent, false);

        ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    @SuppressLint("RecyclerView")
    public void onBindViewHolder(@NonNull ViewHolder holder,  int position) {

        holder.tvTimingTaskReminder.setText("提醒时间" + (position + 1));
        if (size == 2){
            if (position == 0){
                holder.tvTimingTaskTimeSelect.setText(task.getRemind1());
            }
            if (position == 1){
                holder.tvTimingTaskTimeSelect.setText(task.getRemind2());
            }
        }
        if (size == 3){
            if (position == 0){
                holder.tvTimingTaskTimeSelect.setText(task.getRemind1());
            }
            if (position == 1){
                holder.tvTimingTaskTimeSelect.setText(task.getRemind2());
            }
            if (position == 2){
                holder.tvTimingTaskTimeSelect.setText(task.getRemind3());
            }
        }
        holder.rlTimingTaskReminderTimePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.showCardDatePickerDialog(context, "提醒时间选择",  new DialogUtil.OnDateChosenListener() {
                    @RequiresApi(api = Build.VERSION_CODES.O)
                    @Override
                    public void onDateChosen(Long aLong) {
                        if (aLong < DateUtil.getNowTimeLong()){
                            Toast.makeText(context, "选择的提醒时间已过", Toast.LENGTH_SHORT).show();
                        }else {
                            String remind = DateUtil.convertLongToString(aLong, "yyyy年MM月dd日 HH:mm");
                            holder.tvTimingTaskTimeSelect.setText(remind);
                            switch (position){
                                case 0:
                                    task.setRemind1(remind);
                                    break;
                                case 1:
                                    task.setRemind2(remind);
                                    break;
                                case 2:
                                    task.setRemind3(remind);
                                    break;
                            }
                        }
                    }
                });
            }
        });
    }

    @Override
    public int getItemCount() {
        return size;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTimingTaskReminder;
        private TextView tvTimingTaskTimeSelect;
        private RelativeLayout rlTimingTaskReminderTimePicker;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTimingTaskReminder = itemView.findViewById(R.id.tv_timing_task_addReminder);
            tvTimingTaskTimeSelect = itemView.findViewById(R.id.tv_timing_task_timeSelect);
            rlTimingTaskReminderTimePicker = itemView.findViewById(R.id.rl_timing_task_reminder_timePicker);
        }
    }
}
